// NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
// Copyright (C) 2010  Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


/*
 * Login system example, client.
 *
 * This client connects to a front-end server using login system.
 *
 * Before running this client, the front end service sample must run,
 * and also the NeL naming_service, time_service, login_service, welcome_service.
 *
 */

//
// Includes
//

#include <string>
#include <stdio.h>

#include "nel/misc/types_nl.h"
#include "nel/misc/debug.h"
#include "nel/misc/config_file.h"
#include "nel/misc/bit_mem_stream.h"
#include "nel/misc/md5.h"

#include "nel/net/login_client.h"
#include "nel/net/login_cookie.h"
#include "game_share/utils.h"
#include "nel/misc/debug.h"
#include "nel/misc/path.h"
#include <iostream>
#ifndef NL_LS_CFG
#define NL_LS_CFG "."
#endif // NL_LS_CFG

//
// Namespaces
//

using namespace std;
using namespace NLMISC;
using namespace NLNET;

CCallbackClient *login = NULL;
CCallbackClient *fes = NULL;

void cbPlayerInit (NLNET::CMessage& msgin, NLNET::TSockId from, NLNET::CCallbackNetBase &netbase)
{


	NLNET::CMessage msg("SG");
	fes->send(msg);
}

NLNET::TCallbackItem ClientFESCallbackArray[] =
{
	{ "INIT",		cbPlayerInit      },
};

void cbServiceChoose (NLNET::CMessage& msgin, NLNET::TSockId from, NLNET::CCallbackNetBase &netbase)
{
	std::string         ip;
	uint32              port;
	TTicks              session;

	msgin.serial(ip);
	msgin.serial(port);
	msgin.serial(session);

	fes = new CCallbackClient();
	fes->addCallbackArray(ClientFESCallbackArray, sizeof(ClientFESCallbackArray)/sizeof(ClientFESCallbackArray[0]));
	fes->connect(CInetAddress(ip.c_str(),port));

	string user = "test";
	NLNET::CMessage msg("LOGIN");
	msg.serial(user);
	msg.serial(session);
	fes->send(msg);
}

NLNET::TCallbackItem ClientCallbackArray[] =
{
	{ "SC",		cbServiceChoose      },
};

//
// Functions
//

int main (int argc, char **argv)
{
	CApplicationContext myApplicationContext;
	CPath::addSearchPath(NL_LS_CFG);
	CPath::addSearchPath("./script/mission");

	cout<<CPath::getCurrentPath()<<endl;
	cout<<CTime::getSecondsSince1970()<<endl;

	string result;

	CConfigFile ConfigFile;
	ConfigFile.load ("client.cfg");

	string LSHost(ConfigFile.getVar("LSHost").asString());
	string Login = ConfigFile.getVar("Login").asString();

	if(Login.empty())
	{
		char buf[256];
		printf("Login: ");
		Login = fgets(buf, 256, stdin);
	}

	login = new CCallbackClient();
	login->addCallbackArray(ClientCallbackArray, sizeof(ClientCallbackArray)/sizeof(ClientCallbackArray[0]));

	login->connect(CInetAddress("127.0.0.1:49997"));
	string user = "test";
	string password = "test";
	uint32 version = 20130501;

	NLNET::CMessage msg("VLP");
	msg.serial(user);
	msg.serial(password);
	msg.serial(version);
	login->send(msg);

	while (login->connected ())
	{
		login->update ();
		nlSleep(10);
	}

	while ( fes!=NULL && fes->connected ())
	{
		fes->update ();
		nlSleep(10);
	}



	delete login;
	delete fes;

	return 0;
}
